package me.zhiwei.exp9;

import java.sql.*;

public class MySQLOperator {
    // JDBC Driver and DB URL
    private static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static String DB_URL = "jdbc:mysql://localhost:3306/exp9";

    private static String USER = "root";
    private static String PASSWORD = "123456";

    private Connection conn = null;
    private Statement stmt = null;

    public MySQLOperator() {}

    public MySQLOperator(String JBBC_DRIVER, String DB_URL, String USER,
                         String PASSWORD) {
        this.JDBC_DRIVER = JDBC_DRIVER;
        this.DB_URL = DB_URL;
        this.USER = USER;
        this.PASSWORD = PASSWORD;
    }

    public void connect() {
        try{
            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            // 打开链接
            System.out.println("connecting database...");
            conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);
            this.conn = conn;
        } catch (SQLException sqlEx) {
            // 处理 JDBC 错误
            sqlEx.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
    }

    public boolean update(String sql) {
        try {
            this.stmt = this.conn.createStatement();
            this.stmt.executeUpdate(sql);
            return true;
        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
        }
        finally {
            if (this.stmt != null) {
                try {
                    this.stmt.close();
                } catch (SQLException sqlEx) { } // ignore

                this.stmt = null;
            }
        }
        return false;
    }

    public ResultSet select(String sql) {
        try {
            this.stmt = this.conn.createStatement();
            ResultSet rs = this.stmt.executeQuery(sql);
            return rs;
        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
        }
        return null;
    }

    public void disconnect() {
        try{
            if(conn!=null)
                conn.close();
        }catch(SQLException sqlEex){
            sqlEex.printStackTrace();
        }
        System.out.println("Closed connect, GoodBye!");
    }

    public void closeStatement() {
        if (this.stmt != null) {
            try {
                this.stmt.close();
            } catch (SQLException sqlEx) { } // ignore

            this.stmt = null;
        }
    }

    public static void main(String[] args) {
        String table = "BankAccounts";
        MySQLOperator sqlOpera = new MySQLOperator();
        sqlOpera.connect();
//        String sql = String.format("insert into %s (name, cardNumber, balance) " +
//                "VALUES (%s, %s, %f)", table, "\'zhiweio\'", "\'152040135215\'", 10000.00);
        String sql = "select * from " + table;
//        sqlOpera.update(sql);
        try {
            ResultSet rows = sqlOpera.select(sql);
            while (rows.next()) {
                String name = rows.getString("name");
                System.out.println(name);
            }
            rows.close();
        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
        } finally {
            sqlOpera.closeStatement();
            sqlOpera.disconnect();
        }
    }
}
